﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DotSpatial.Controls;
using DotSpatial.Symbology;
using DotSpatial.Data;
using DotSpatial.Topology;

using System.Windows.Forms;




namespace HaGisPack
{
   public  class MFSystem:SubSystem 
    {

       public MFSystem( GisManager p)
           : base(p)
       {

           _geoMap = p.CurrentMap ;

           _layerSys = p.LayerSys;
       }



      


       private IMap _geoMap;
       LayerSystem _layerSys;

       FLayer _currentLayer;


    


       //下面定义了一些常用的类型
       SquareMF m_Square;

       RectangleMF m_Rectangle;

       CircleMF m_Circle;

       AddShapeMF m_Shape;

       SelectAndMoveMF m_SelectShape;

       MoveShapeMF m_MoveShape;

       FourSideMF m_FourSide;  //四边形

       TwopointLineMF m_TwoptLine; //两点线

       RectangleMF2 m_rect2;

       List<string> _SelectedID;

       /// <summary>
       /// 存储所选择的图形的ID
       /// </summary>
       public List<string> SelectedID
       {
           get { return _SelectedID; }
           set { _SelectedID = value; }
       }






       public void UseFunctionTool(FunctionTool selfunc)
       {

           if (selfunc != FunctionTool.SELECT)
           {
               _currentLayer = this._layerSys.CurrentLayer;

               if (_currentLayer == null)
               {
                   return;
               }
           }
           if (_geoMap ==null )
           {
               return;
               
           }

           switch (selfunc)
           {
               case FunctionTool.SQUARE:
                   this.UseSquareMF();
                   break;
               case FunctionTool.CIRCLE:

                   this.UseCircleMF();

                   break;
               case FunctionTool.RECTANGLE:

                   this.UseRectangleMF();

                   break;
               case FunctionTool.lINE:
                   this.UseTwoptLineMF(FeatureType.Line);
                   break;
               case FunctionTool.POLYGON:
                   this.UseShapeMF(FeatureType.Polygon);
                   break;
               case FunctionTool.POINT:
                   this.UseShapeMF(FeatureType.Point);
                   break;

               case FunctionTool .FourPtPOLYGON :
                   this.UseFourSideMf();  //四边形
                   break;

               case FunctionTool .RECTANGLE2 :
                   this.UseRectangleMF2();
                   break;

               ///其他主功能区
               ///

               case FunctionTool.MOVE:

                   //Feature f = this.GetSingleFeature();
                   //if (f!=null )
                   //{


                   //}


                   break;

               case FunctionTool.SELECT:

                   this.UseSelectMF();
                   break;

               default:
                   break;

           }



 
       }






       protected Feature GetSingleFeature()
       {
           //取得所有的已经选则的feature

           List<Feature> features = new List<Feature>();

           foreach (KeyValuePair<string, FLayer> lay in this._layerSys.Layers)
           {
               foreach (DotSpatial.Data.Feature f in (lay.Value.ILayer as IFeatureLayer).Selection.ToFeatureList())
               {
                   features.Add(f);
               }
           }


           frmSingleFeatureSelect frmsf = new frmSingleFeatureSelect(features);

           if (frmsf.ShowDialog() == DialogResult.OK)
           {
               return frmsf.SelectedFeature;

           }
           else
           {
               return null;

           }

       }


       protected void UseRectangleMF2()
       {
           if (m_rect2 == null)
           {
               m_rect2 = new RectangleMF2(_geoMap);
               m_rect2.Name = "Rectangle2";
               m_rect2.Parent = this;


           }
           if (_geoMap.MapFunctions.Contains(m_rect2) == false)
           {
               _geoMap.MapFunctions.Add(m_rect2);
           }
           _geoMap.FunctionMode = FunctionMode.None;
           _geoMap.Cursor = Cursors.Hand;
           /// _geoMap.MapFrame.LayerSelected += MapFrame_LayerSelected;


           m_rect2.Activate();

       }

       protected void UseRectangleMF()
       {
       
          
           if (m_Rectangle == null)
           {
               m_Rectangle = new RectangleMF(_geoMap);
               m_Rectangle.Name = "Rectangle";
               m_Rectangle.Parent = this;


           }
           if (_geoMap.MapFunctions.Contains(m_Rectangle) == false)
           {
               _geoMap.MapFunctions.Add(m_Rectangle);
           }
           _geoMap.FunctionMode = FunctionMode.None;
           _geoMap.Cursor = Cursors.Hand;
          
          
           m_Rectangle.Activate();
       }



       protected void UseCircleMF()
       {
        
           if (m_Circle == null)
           {
               m_Circle = new CircleMF(_geoMap);
               m_Circle.Name = "Circle";
               m_Circle.Parent = this;



           }
           if (_geoMap.MapFunctions.Contains(m_Circle) == false)
           {
               _geoMap.MapFunctions.Add(m_Circle);
           }
           _geoMap.FunctionMode = FunctionMode.None;
           _geoMap.Cursor = Cursors.Hand;
         

         
           m_Circle.Activate();


       }

       protected void UseSquareMF()
       {
        
           if (m_Square == null)
           {
               m_Square = new SquareMF(_geoMap);
               m_Square.Name = "Square";
               m_Square.Parent = this;


           }
           if (_geoMap.MapFunctions.Contains(m_Square) == false)
           {
               _geoMap.MapFunctions.Add(m_Square);
           }
           _geoMap.FunctionMode = FunctionMode.None;
           _geoMap.Cursor = Cursors.Hand;
           /// _geoMap.MapFrame.LayerSelected += MapFrame_LayerSelected;

        
           m_Square.Activate();



       }


       protected void UseShapeMF(FeatureType tp)
       {
         
           if (m_Shape == null)
           {
               m_Shape = new AddShapeMF(_geoMap);
               m_Shape.Name = "Shape";
               m_Shape.Parent = this;


           }
           if (_geoMap.MapFunctions.Contains(m_Shape) == false)
           {
               _geoMap.MapFunctions.Add(m_Shape);
           }
           _geoMap.FunctionMode = FunctionMode.None;
           _geoMap.Cursor = Cursors.Hand;

           m_Shape.FeatureType = tp;
           m_Shape.Activate();
       }


       protected void UseTwoptLineMF(FeatureType tp)
       {

           if (m_TwoptLine == null)
           {
               m_TwoptLine = new TwopointLineMF(_geoMap);
               m_TwoptLine.Name = "TwoptLine";
               m_TwoptLine.Parent = this;


           }
           if (_geoMap.MapFunctions.Contains(m_TwoptLine) == false)
           {
               _geoMap.MapFunctions.Add(m_TwoptLine);
           }
           _geoMap.FunctionMode = FunctionMode.None;
           _geoMap.Cursor = Cursors.Hand;

           m_TwoptLine.FeatureType = tp;
           m_TwoptLine.Activate();
       }

       protected void UseSelectMF()
       {

           if (m_SelectShape == null)
           {
               m_SelectShape = new SelectAndMoveMF(this._geoMap, this.Parent.LayerSys.GetEditableFeatureLayers());
               m_SelectShape.Name = "SelectShape";
               m_SelectShape.FunctionBase.Parent = this;

           }
           if (_geoMap.MapFunctions.Contains(m_SelectShape) == false)
           {
               _geoMap.MapFunctions.Add(m_SelectShape);
           }
           _geoMap.FunctionMode = FunctionMode.None;
           //_geoMap.Cursor = new Cursor(@"F:\MapWindow6\mapwindow6_dbc476e1b259\MapWindow6Orlando\Resources\select.ico");
           _geoMap.Cursor = Cursors.Hand;

           m_SelectShape.Activate();


       }

       public void UseMoveMF(List<IFeatureLayer> fl)
       {

           if (m_MoveShape == null)
           {
               m_MoveShape = new MoveShapeMF(_geoMap);
               m_MoveShape.Name = "MoveShape";
               m_MoveShape.Parent = this;
           }
           if (_geoMap.MapFunctions.Contains(m_MoveShape) == false)
           {
               _geoMap.MapFunctions.Add(m_MoveShape);
           }
           _geoMap.FunctionMode = FunctionMode.None;
           _geoMap.Cursor = Cursors.Hand;

           m_MoveShape.Layer = fl;

           m_MoveShape.Activate();

       }



       public void UseFourSideMf()
       {
           if (this.m_FourSide == null)
           {
               this.m_FourSide = new FourSideMF(_geoMap);
               m_FourSide.Name = "FourSide";
               m_FourSide.Parent = this;
           }
           if (_geoMap.MapFunctions.Contains(m_FourSide) == false)
           {
               _geoMap.MapFunctions.Add(m_FourSide);
           }
           _geoMap.FunctionMode = FunctionMode.None;
           _geoMap.Cursor = Cursors.Hand;

           m_FourSide.Activate();
       
       }








       #region 对外编辑事件，以及对应的处理函数

       public event ShapeHandler ShapeAdded;

       public void AskShapeAdd(FShape shp)
       {

           if (ShapeAdded != null)
           {
               shp.LayerName = this._layerSys.CurrentLayer.Id;
               ShapeAdded(shp);
           }


       }


       //刘加
       public event ShapeHandler ShapeCopyed;

       public void AskShapeCopyed(FShape shp)
       {
           if (ShapeCopyed != null)
           {
               //shp.LayerName = this._layerSys.CurrentLayer.Id;
               ShapeCopyed(shp);
           }
       }

      



       /// <summary>
       /// 对应有移动，和变更点位置
       /// </summary>
       public event ShapeHandler ShapeChanged;

       public void AskShapeChanged(FShape shp)
       {
           if (ShapeChanged != null)
           {
               shp.LayerName = this._layerSys.CurrentLayer.Id;
               ShapeChanged(shp);

           }
       }


       public event ShapeHandler ShapeRemoved;

       public void AskShapeRemoved(FShape shp)
       {

           if (ShapeRemoved != null)
           {
               shp.LayerName = this._layerSys.CurrentLayer.Id;
               ShapeRemoved(shp);

           }
       }






       #endregion



    }
}
